import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mathfun/pages/dot_flash_training_page.dart';
import 'features/training_records/model/training_record.dart';
import 'features/training_records/repository/training_record_repository.dart';
import 'features/training_records/bloc/training_history_bloc.dart';
import 'pages/home_page.dart';
import 'pages/dot_training_page.dart';
import 'pages/audio_training_page.dart';
import 'pages/settings_page.dart';
import 'pages/make_ten_page.dart';
import 'utils/responsive_util.dart';

void main() async {
  print('Starting app initialization...');
  try {
    await Hive.initFlutter();
    print('Hive initialized successfully');
    Hive.registerAdapter(TrainingRecordAdapter());
    print('TrainingRecordAdapter registered');
    final repository = TrainingRecordRepository();
    await repository.init();
    print('TrainingRecordRepository initialized successfully');
  } catch (e) {
    print('Error during initialization: $e');
  }

  runApp(
    BlocProvider(
      create: (context) {
        try {
          final repository = TrainingRecordRepository();
          repository.init();
          return TrainingHistoryBloc(repository: repository);
        } catch (e) {
          print('Error creating TrainingHistoryBloc: $e');
          return TrainingHistoryBloc(repository: TrainingRecordRepository());
        }
      },
      child: MathFunApp(),
    ),
  );
}

class MathFunApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    ResponsiveUtil.init(context);
    return MaterialApp(
      title: '数感小达人',
      theme: ThemeData(
        primarySwatch: Colors.orange,
        colorScheme: ColorScheme.fromSwatch().copyWith(
          primary: Colors.orange,
          secondary: Colors.deepOrange,
        ),
        scaffoldBackgroundColor: Colors.grey[100],
        cardTheme: CardTheme(
          elevation: 4,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(16),
          ),
        ),
        elevatedButtonTheme: ElevatedButtonThemeData(
          style: ElevatedButton.styleFrom(
            foregroundColor: Colors.white,
            backgroundColor: Colors.orange,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(12),
            ),
            elevation: 4,
            padding: EdgeInsets.symmetric(vertical: 16, horizontal: 24),
          ),
        ),
        fontFamily: 'Arial',
      ),
      initialRoute: '/',
      routes: {
        '/': (context) => HomePage(),
        '/dot': (context) => DotTrainingPage(),
        '/audio': (context) => AudioTrainingPage(),
        '/settings': (context) => SettingsPage(),
        '/make_ten': (context) => MakeTenPage(),
        '/dot_flash': (context) => DotFlashTrainingPage(),
      },
    );
  }
}
